#ifndef __DIALOG_H__
#define __DIALOG_H__

#include "babyfish/event.h"
#include "gofgame/common/WeakRef.h"
#include "gofgame/common/ui/DialogClosedEventArgs.h"
#include "gofgame/common/ui/Container.h"
#include "gofgame/common/ui/Image.h"
#include "gofgame/common/ui/Label.h"
#include "gofgame/common/ui/Button.h"

namespace gofgame {
	namespace common {
		namespace ui {

			using namespace babyfish;
			using namespace gofgame::common::ui;

			class Dialog : public Container {
			public:
				Dialog();

				virtual bool init() _override;

				virtual bool isModal() const;

				virtual const char *getTitle() const;
				virtual void setTitle(const char *title);

				virtual Brush *getTitleBrush() const;
				virtual void setTitleBrush(Brush *titleBrush);
				virtual Brush *getTitleBackgroundBrush() const;
				virtual void setTitleBackgroundBrush(Brush *titleBackgroundBrush);
				virtual Brush *getCloseButtonBackgroundBrush() const;
				virtual void setCloseButtonBackgroundBrush(Brush *closeButtonBackgroundBrush);
				virtual Brush *getCloseButtonPressedBackgroundBrush() const;
				virtual void setCloseButtonPressedBackgroundBrush(Brush *closeButtonPressedBackgroundBrush);
				virtual Brush *getContentBackgroundBrush() const;
				virtual void setContentBackgroundBrush(Brush *contentBackgroundBrush);
				virtual void setBackgroundBrush(Brush *backgroundBrush) _override _final;

				virtual float getTitleHeight() const;
				virtual void setTitleHeight(float titleBackgroundHeight);

				virtual float getBorderWidth() const;
				virtual void setBorderWidth(float borderWidth);

				virtual void show(bool modal = true, CCScene *scene = nullptr);
				virtual void show(CCScene *scene);
				virtual void close(DialogResult dialogResult = DialogResult_None);

				virtual void draw() _override;

				virtual bool isDragable() const;
				virtual void setDragable(bool dragable);

				declare_event(event_handler<DialogClosedEventArgs>, closed, _closed);

				replace_children_management("this->getContentContainer()");

			protected:
				virtual void onDispatchManualBubbleEvent(ManualBubbleEventArgs &e) _override;
				virtual void onValidateContainer(Container *container) _override;
				Container *getContentContainer() const;

				virtual void onDialogClosed(DialogClosedEventArgs &e);
			private:
				void closeButton_click(void *sender, ComponentEventArgs &e);

			private:
				class TitleContainer : public Container {
				public:
					TitleContainer(Dialog *owner);

				protected:
					virtual void onTouchBegan(TouchEventArgs &e) _override;
					virtual void onTouchMoved(TouchEventArgs &e) _override;
					
				private:
					WeakRef<Dialog> owner;
					CCPoint touchDialogBeganLocation;
				};

			private:
				event_handler<DialogClosedEventArgs> _closed;
				Ref<TitleContainer> titleContainer;
				Ref<Label> titleLabel;
				Ref<Button> closeButton;
				Ref<Container> contentContainer;
				float borderWidth;
				bool modal;
				bool dragable;
			};
		}
	}
}

#endif //__DIALOG_H__
